# 修改 asset_views.py 中的部分代码
from rest_framework.response import Response
from rest_framework.views import APIView
from demo_one.models import Asset
from demo_one.serializers import AssetSerializer
from .pagination import StandardPagination


class AssentListView(APIView):
    def get(self, request):
        # 获取所有资产并按采购时间降序排列
        assets = Asset.objects.all().order_by('-id')

        # 初始化分页器
        paginator = StandardPagination()

        # 对查询集进行分页
        page = paginator.paginate_queryset(assets, request)

        if page is not None:
            # 处理分页数据
            serializer = AssetSerializer(page, many=True)
            # 使用标准的 DRF 分页响应格式
            return paginator.get_paginated_response(serializer.data)

        # 未分页时的处理（当page_size=0或不启用分页时）
        serializer = AssetSerializer(assets, many=True)
        return Response({
            "results": {
                "code": 200,
                "message": "资产查询成功",
                "data": serializer.data,
                "total": assets.count()
            }
        })
